﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using ARR.Entities;
using ARR.UI.UnitsOfMeasureService;
using DDF.Entity;
using ARR.UI.ArticlesService;

namespace ARR.UI.Models
{
    public interface IArticleMasterChangeUomPopupModel : IModelBase
    {
        ObservableCollection<UnitOfMeasureConvertion> UnitsOfMeasure { get; set; }

        void Save(UnitOfMeasureConvertion selectedConvertion);

        void LoadUnitsOfMeasure();

        UnitOfMeasureConvertion CreateNewUomConvertion();
    }

    public class ArticleMasterChangeUomPopupModel : ModelBase, IArticleMasterChangeUomPopupModel
    {
        private readonly List<Article> articles;

        public ArticleMasterChangeUomPopupModel(List<Article> articles)
        {
            this.articles = articles;

            UnitsOfMeasure = new ObservableCollection<UnitOfMeasureConvertion>();

            LoadUnitsOfMeasure();
        }


        public ObservableCollection<UnitOfMeasureConvertion> UnitsOfMeasure { get; set; }

        public void Save(UnitOfMeasureConvertion selectedConvertion)
        {
            var toSave = UnitsOfMeasure.Where(x => x.GetEntityState() != EntityState.Original);

            if (toSave.Any())
            {
                UnitsOfMeasureServiceClient service = new UnitsOfMeasureServiceClient();

                try
                {
                    service.Save(toSave.ToList());
                    LoadUnitsOfMeasure();

                    service.Close();
                }
                catch
                {
                    service.Abort();
                    throw;
                }
            }

            var convertion = UnitsOfMeasure.SingleOrDefault(x => x.SourceUnit == selectedConvertion.SourceUnit &&
                                                                 x.TargetUnit == selectedConvertion.TargetUnit &&
                                                                 x.Value == selectedConvertion.Value);
            if (convertion != null)
            {
                foreach (var art in articles)
                {
                    art.UnitOfMeasureConvertionId = convertion.Id;
                }

                ArticlesServiceClient service = new ArticlesServiceClient();

                try
                {
                    service.SaveArticles(articles);
                    service.Close();
                }
                catch
                {
                    service.Abort();
                    throw;
                }
            }
        }

        public void LoadUnitsOfMeasure()
        {
            UnitsOfMeasureServiceClient service = new UnitsOfMeasureServiceClient();

            try
            {
                UnitsOfMeasure = new ObservableCollection<UnitOfMeasureConvertion>(service.GetBySourceUnit(articles.First().UOM));
                service.Close();
            }
            catch
            {
                service.Abort();
                throw;
            }
        }

        public UnitOfMeasureConvertion CreateNewUomConvertion()
        {
            return new UnitOfMeasureConvertion() { SourceUnit = articles.First().UOM };
        }
    }

    public class ArticleMasterChangeUomPopupModelMock : MockModelBase, IArticleMasterChangeUomPopupModel
    {
        private readonly List<Article> articles;

        public ArticleMasterChangeUomPopupModelMock(List<Article> articles)
        {
            this.articles = articles;

            UnitsOfMeasure = new ObservableCollection<UnitOfMeasureConvertion>();
        }

        public ObservableCollection<UnitOfMeasureConvertion> UnitsOfMeasure { get; set; }

        public void Save(UnitOfMeasureConvertion selectedConvertion)
        {

        }

        public void LoadUnitsOfMeasure()
        {
            UnitsOfMeasure.Add(new UnitOfMeasureConvertion { SourceUnit = "Gramos", TargetUnit = "Miligramos", Value = 1000m });
            UnitsOfMeasure.Add(new UnitOfMeasureConvertion { SourceUnit = "Miligramos", TargetUnit = "Gramos", Value = 1000m });
            UnitsOfMeasure.Add(new UnitOfMeasureConvertion { SourceUnit = "Litros", TargetUnit = "Botellas", Value = 2.25m });
            UnitsOfMeasure.Add(new UnitOfMeasureConvertion { SourceUnit = "Botellas", TargetUnit = "Litros", Value = 2.25m });
        }

        public UnitOfMeasureConvertion CreateNewUomConvertion()
        {
            return new UnitOfMeasureConvertion() { SourceUnit = articles.First().UOM };
        }
    }
}
